package com.rp.article.serivce.impl;

import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rp.article.mapper.CommentsMapper;
import com.rp.article.serivce.ArticlePortalService;
import com.rp.article.serivce.CommentPortalService;
import com.rp.news.common.PagedResult;
import com.rp.news.util.RedisOperator;
import com.rp.pojo.Comments;
import com.rp.vo.ArticleDetailVO;
import com.rp.vo.CommentsVO;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentPortalServiceImpl implements CommentPortalService {

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private ArticlePortalService articlePortalService;


    @Autowired
    private RedisOperator redis;


    public static final String REDIS_ARTICLE_COMMENT_COUNTS = "redis_article_comment_counts:";

    @Override
    public void createCommnet(String articleId, String fatherId, String content, String userId, String nickname,String face) {

        ArticleDetailVO detail = articlePortalService.detail(articleId);

        Comments comments = new Comments();
        comments.setId(IdUtil.fastSimpleUUID());

        comments.setWriterId(detail.getPublishUserId());
        comments.setArticleTitle(detail.getTitle());
        comments.setArticleCover(detail.getCover());
        comments.setArticleId(articleId);

        comments.setFatherId(fatherId);
        comments.setCommentUserId(userId);
        comments.setCommentUserNickname(nickname);
        comments.setCommentUserFace(face);

        comments.setContent(content);
        comments.setCreateTime(new Date());
        commentsMapper.insert(comments);

        redis.increment(REDIS_ARTICLE_COMMENT_COUNTS+ articleId,1);
    }

    @Override
    public PagedResult queryArticleComments(String articleId, Integer page, Integer pageSize) {


        Map<String, Object> map = new HashMap<>();
        map.put("articleId",articleId);

        PageHelper.startPage(page,pageSize);
        List<CommentsVO> commentsVOS = commentsMapper.queryArticleCommnetList(map);

        return setterPageResult(commentsVOS,page);
    }

    @Override
    public PagedResult mng(String writerId, Integer page, Integer pageSize) {

        Comments comment = new Comments();
        comment.setWriterId(writerId);

        PageHelper.startPage(page,pageSize);
        List<Comments> select = commentsMapper.select(comment);

        return setterPageResult(select,page);
    }

    @Override
    public void delete(String writerId, String commentId) {
        Comments comment =  new Comments();
        comment.setWriterId(writerId);
        comment.setId(commentId);
        commentsMapper.delete(comment);
    }


    public PagedResult setterPageResult(List<?> list, Integer page){
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedResult result = new PagedResult();
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }
}
